'use strict';

const Controller = require('egg').Controller;
class CarouselController extends Controller {
  //添加轮播图
  /**
     * @api {Post} /api/carousel 添加轮播图(管理台)
     * @apiGroup 轮播图
     * @apiParam {String}  authorization ⚡登录成功后的token值（放在headers）
     * @apiParam {String}  photo_id   ⚡图片id
     * @apiParam {String}  hyperlink  // 超链接
     * @apiParam {INTEGER}  site_id   // 位置id 
     * @apiParam {INTEGER}  sort      // 排序   默认999
     * @apiParamExample 参数模板
      {
      "photo_id": "1",
      "hyperlink": "htpp://www.baidu.com",
      "site_id": 1,
      "sort": 999,
      }
     * @apiSuccessExample 成功返回
     * {
     *    "msg":'添加成功'
     * }
     *
     * @apiErrorExample 错误返回
     * {
     *  "msg":"添加失败"
     * }
     *
     */
  async create() {
    const { ctx, service } = this;

    /** 验证photo_id是否合法 */
    const checkRes = await new ctx.helper.checkData(this, 'photo_id');
    if (!checkRes.checkDataRes) {
      return;
    }

    try {
      const result = await service.shop.carousel.create();
      if (result.dataValues) {
        ctx.body = ctx.response.success('添加成功');
      } else {
        ctx.body = ctx.response.error(result, '添加失败');
      }
    } catch (error) {
      ctx.body = ctx.response.error(error, '添加失败');
    }
  }
  // 获取所有轮播图
  /**
     * @api {Get} /api/carousel 获取所有轮播图
     * @apiGroup 轮播图
     * @apiParam {String}  site_id       位置id(默认所有位置)  1 是顶部, 2 是中部, 3 是尾部
     * @apiParam {INTEGER}  project_id   ⚡项目id
     * @apiParamExample 参数模板
      {
        "project_id":1
      }
     * @apiSuccessExample  成功返回
      {
        "msg": "成功操作",
        "result": {
            "count": 2,
            "rows": [
                {
                "id": 7,  //轮播图id，用于获取单个轮播图
                "user_id": 1,
                "project_id": 1,
                "photo_id": 10,
                "sort": "999",
                "hyperlink": "",
                "site_id": "1",
                "createdAt": "2021-08-18T02:06:38.000Z",
                "updatedAt": "2021-08-18T02:06:38.000Z",
                "s_photos": [
                    {
                        "id": 10,
                        "point_id": 7,
                        "user_id": 1,
                        "project_id": 1,
                        "sort_id": "99999",
                        "type": "3",
                        //轮播图地址
                        "path": "http://shops-1251395798.cos.ap-nanjing.myqcloud.com/1629252393994-5d0cbb64eb9af8a32abd8cb2a2b0f3eb.jpg",
                        "createdAt": "2021-08-18T02:06:34.000Z",
                        "updatedAt": "2021-08-18T02:06:38.000Z"
                    }
                ]
            },
            {
                "id": 8,
                "user_id": 1,
                "project_id": 1,
                "photo_id": 11,
                "sort": "999",
                "hyperlink": "",
                "site_id": "1",
                "createdAt": "2021-08-18T02:06:50.000Z",
                "updatedAt": "2021-08-18T02:06:50.000Z",
                "s_photos": [
                    {
                        "id": 11,
                        "point_id": 8,
                        "user_id": 1,
                        "project_id": 1,
                        "sort_id": "99999",
                        "type": "3",
                        //轮播图地址
                        "path": "http://shops-1251395798.cos.ap-nanjing.myqcloud.com/1629252408392-8ec0ed2abfdef52deb71c7280ffe3c56.jpeg",
                        "createdAt": "2021-08-18T02:06:48.000Z",
                        "updatedAt": "2021-08-18T02:06:50.000Z"
                    }
                ]
            },
            ]
        }
    }
     * @apiErrorExample  错误返回
     * {
     *   "msg": "获得所有轮播图信息失败"
     * }
     */
  async index() {
    const { ctx, app, service } = this;
    try {
      const res = await service.shop.carousel.index();

      if (res.length === 0 || res[0].dataValues) {
        ctx.body = ctx.response.success(res);
      } else {
        ctx.body = ctx.response.error(res);
      }
    } catch (error) {
      ctx.body = ctx.response.error(error.message);
    }
  }
  // 获取轮播图单个信息
  /**
     * @api {Get} /api/carousel/:id 获取轮播图单个信息
     * @apiGroup 轮播图
     * @apiParam {INTEGER}  project_id   ⚡项目id
     * @apiParam {INTEGER}  id          ⚡轮播图id
     * @apiParamExample 参数模板
      {
        "project_id":1
      }
     * @apiSuccessExample  成功返回
    {
        "msg": "成功操作",
        "result": {
            "id": 1,
            "photo_id": "165",
            "sort": 999,
            "hyperlink": "http://www.baidu.com",
            "site_id": "0",
            "createdAt": "2020-08-04T07:58:09.000Z",
            "updatedAt": "2020-08-04T09:22:51.000Z",
            "s_photos": [
                {
                    "id": 165,
                    "point_id": 1,
                    "sort_id": 99,
                    "type": 3,
                    //轮播图图片地址
                    "path": "\\public\\uploads\\1596528708968-4a47a0db6e60853dedfcfdf08a5ca249.png",
                    "createdAt": "2020-08-04T08:11:48.000Z",
                    "updatedAt": "2020-08-04T09:22:54.000Z"
                }
            ]
        }
    }
     * @apiErrorExample  错误返回
     * {
     *   "msg": "找不到该商品分类信息"
     * }
     */

  async show() {
    const { ctx } = this;
    const id = ctx.query.id;

    try {
      const { Carousel, Photo } = this.app.model.Tables.Shop;
      const result = await Carousel.findOne({
        where: {
          id,
        },
        include: [
          {
            // 需要的字段
            // attributes: [ 'id', 'path' ],
            model: Photo,
          },
        ],
      });
      if (result) {
        ctx.body = ctx.response.success(result);
      } else {
        ctx.body = ctx.response.error('找不到该轮播图信息');
      }
    } catch (error) {
      ctx.body = ctx.response.error(error.message);
    }
  }
  // 更新轮播图
  /**
     * @api {put} /api/carousel/:id 更新轮播图(管理台)
     * @apiGroup 轮播图
     * @apiParam {String}  url        ⚡图片url    
     * @apiParam {String}  hyperlink  超链接
     * @apiParam {INTEGER}  site_id   位置id 默认0
     * @apiParam {INTEGER}  sort      排序   默认999
     * @apiParamExample 参数模板
     {
      "url": "htpp://www.baidu.com",
      "hyperlink": "htpp://www.baidu.com",
      "site_id": 0,
      "sort": 1,
      }
     * @apiSuccessExample  成功返回
     {
        "msg": "成功操作",
        "result": {
            "id": 1,
            "url": "https://dss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2534506313,1688529724&fm=26&gp=0.jpg",
            "sort": 999,
            "hyperlink": "http://www.baidu.com",
            "site_id": "0",
            "createdAt": "2020-08-04T06:54:11.000Z",
            "updatedAt": "2020-08-04T06:54:11.000Z"
        }
    }
     * @apiErrorExample  错误返回
     * {
     *   "msg": "找不到该轮播图"
     * }
     */

  async update() {
    const { ctx, service } = this;
    const dataObj = ctx.request.body;
    dataObj.hyperlink = dataObj.hyperlink || null;
    if (new ctx.helper.checkData(this).checkDataRes) {
      const id = ctx.params.id;
      try {
        const { Carousel } = this.app.model.Tables.Shop;
        const resultAdmin = await Carousel.findByPk(id);
        if (resultAdmin && resultAdmin.dataValues) {
          const result = await resultAdmin.update(dataObj);
          const photoResult = await service.shop.photo.update(
            result.dataValues.id,
            dataObj.photo_id,
            3
          );

          ctx.body = ctx.response.success(result.dataValues);
        } else {
          ctx.body = ctx.response.error('找不到该轮播图');
        }
      } catch (error) {
        ctx.body = ctx.response.error(error.message);
      }
    }
  }

  /**
     * @api {delete} /api/carousel/:id 删除轮播图(管理台)
     * @apiGroup 轮播图
     * @apiParam {String}   id   ⚡轮播图id
     * @apiParamExample 参数模板
     {}
     * @apiSuccessExample 成功返回
     * {
     *    "msg":'删除成功'
     * }
     *
     * @apiErrorExample 错误返回
     * {
     *  "msg":"删除失败"
     * }
     *
     */

  async destroy() {
    const { ctx } = this;
    const id = ctx.params.id;
    try {
      const { ctx } = this;
      const { Carousel } = this.app.model.Tables.Shop;
      const result = await Carousel.destroy({
        where: {
          id,
        },
      });
      ctx.body =
        result > 0
          ? ctx.response.success('删除成功')
          : ctx.response.error(result, '删除失败');
    } catch (error) {
      ctx.body = this.ctx.response.error(error.message);
    }
  }
}

module.exports = CarouselController;
